package com.chx.three.mama.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chx.three.mama.R;
import com.chx.three.mama.utils.DensityUtil;

import org.w3c.dom.Attr;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 标题栏的选择框
 */
public class ActionBarSelectLayout extends RelativeLayout {
    TextView leftText;
    TextView rightText;
    TextView centerText;
    ImageView imageView;
    LinearLayout layout;
    int mScrollWidth;
    //是否是第一次测量
    boolean hasFirstMeasure;
    int leftMargin;
    Context mContext;
    String leftTitle = "";
    String rightTitle = "";
    String centerTitle = "";
    private int mCount;

    private LayoutParams imageParams;
    private int textSize;

    public ActionBarSelectLayout(Context context) {
        this(context, null);
    }

    public ActionBarSelectLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ActionBarSelectLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setBackgroundResource(R.drawable.a_shape_topbar_bg);
        this.mContext = context;
        initData(attrs);
        initView();

    }

    private void initData(AttributeSet attrs){
        TypedArray ta = mContext.obtainStyledAttributes(attrs, R.styleable.topSelect);
         rightTitle = ta.getString(R.styleable.topSelect_rightTitle);
        leftTitle=ta.getString(R.styleable.topSelect_leftTitle);
        centerTitle=ta.getString(R.styleable.topSelect_centerTitle);
        textSize=(int)ta.getDimension(R.styleable.topSelect_textSize,12);
        textSize=DensityUtil.px2dip(mContext,textSize);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (!hasFirstMeasure) {
            int padding = DensityUtil.dip2px(mContext, 2);
            setPadding(padding, padding, padding, padding);
            mCount = 2;
            if (!TextUtils.isEmpty(centerTitle)) {
                mCount++;
            }
            int width = MeasureSpec.getSize(widthMeasureSpec);
            width = width / mCount;
            mScrollWidth = width;
            imageParams = new LayoutParams(width, ViewGroup.LayoutParams.MATCH_PARENT);
            imageView.setLayoutParams(imageParams);
            hasFirstMeasure = true;
        }

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

    private void initView() {
        this.imageView = new ImageView(mContext);
        this.leftText = new TextView(mContext);
        this.rightText = new TextView(mContext);
        this.centerText = new TextView(mContext);

        leftText.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onTopTitleSelected != null) {
                    onTopTitleSelected.onLeftTitleClick();
                }
                leftMargin = 0;
                imageParams.leftMargin=leftMargin;
                imageView.setLayoutParams(imageParams);
                clearTitleColor();
                leftText.setTextColor(Color.WHITE);
            }
        });
        rightText.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onTopTitleSelected != null) {
                    onTopTitleSelected.onRightTitleClick();
                }
                leftMargin = mScrollWidth * (mCount - 1);
                imageParams.leftMargin=leftMargin;
                imageView.setLayoutParams(imageParams);
                clearTitleColor();
                rightText.setTextColor(Color.WHITE);

            }
        });
        centerText.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onTopTitleSelected != null) {
                    onTopTitleSelected.onCenterTitleClick();
                }
                leftMargin = mScrollWidth;
                imageParams.leftMargin=leftMargin;
                imageView.setLayoutParams(imageParams);
                clearTitleColor();
                centerText.setTextColor(Color.WHITE);
            }
        });

        clearTitleColor();
        leftText.setTextColor(Color.WHITE);
        leftText.setTextSize(textSize);
        rightText.setTextSize(textSize);
        centerText.setTextSize(textSize);

        leftText.setText(leftTitle);
        if(!TextUtils.isEmpty(centerTitle)){
            centerText.setVisibility(View.VISIBLE);
        }else{
            centerText.setVisibility(View.GONE);
        }
        centerText.setText(centerTitle);
        rightText.setText(rightTitle);
        layout = new LinearLayout(mContext);

        getWidth();

        imageView.setBackgroundResource(R.drawable.a_shape_topbar_selected);
        layout.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        textParams.weight = 1;

        leftText.setGravity(Gravity.CENTER);
        centerText.setGravity(Gravity.CENTER);
        rightText.setGravity(Gravity.CENTER);
        leftText.setLayoutParams(textParams);
        centerText.setLayoutParams(textParams);
        rightText.setLayoutParams(textParams);
        layout.setGravity(Gravity.CENTER_VERTICAL);
        leftText.setLayoutParams(textParams);
        layout.setBackgroundColor(Color.parseColor("#00000000"));
        addView(imageView);
        layout.addView(leftText);
        layout.addView(centerText);
        layout.addView(rightText);
        addView(layout);
    }


    private void clearTitleColor() {
        leftText.setTextColor(Color.parseColor("#ffff6f84"));
        rightText.setTextColor(Color.parseColor("#ffff6f84"));
        centerText.setTextColor(Color.parseColor("#ffff6f84"));
    }

    private OnTopTitleSelected onTopTitleSelected;

    public interface OnTopTitleSelected {
        void onRightTitleClick();

        void onLeftTitleClick();

        void onCenterTitleClick();
    }

    public void setOnTopTitleSelected(OnTopTitleSelected onTopTitleSelected) {
        this.onTopTitleSelected = onTopTitleSelected;
    }
}
